local xizheng = fk.CreateSkill {
  name = "lingling__xizheng",
}

Fk:loadTranslationTable{
  ["lingling__xizheng"] = "西征",
  [":lingling__xizheng"] = "准备阶段，你可以弃置一张【闪】并判定，若为黑色，本回合你下一次造成的伤害+1，若为红色，本回合下一次牌被弃置时你获得之。",

  ["#lingling__xizheng-invoke"] = "西征：你可以弃一张【闪】并判定，根据颜色本回合获得效果",
  ["@@lingling__xizheng_black-turn"] = "西征 伤害+1",
  ["@@lingling__xizheng_red-turn"] = "西征 获得弃牌",
}

xizheng:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(xizheng.name) and player.phase == Player.Start and
      not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local card = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = false,
      skill_name = xizheng.name,
      pattern = "jink",
      prompt = "#lingling__xizheng-invoke",
      cancelable = true,
      skip = true,
    })
    if #card > 0 then
      event:setCostData(self, {cards = card})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).cards, xizheng.name, player, player)
    if player.dead then return end
    local judge = {
      who = player,
      reason = xizheng.name,
      pattern = ".|.|^nosuit",
    }
    room:judge(judge)
    if player.dead then return end
    room:addPlayerMark(player, "@@lingling__xizheng_"..judge.card:getColorString().."-turn", 1)
  end,
})

xizheng:addEffect(fk.DamageCaused, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@lingling__xizheng_black-turn") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    data:changeDamage(player:getMark("@@lingling__xizheng_black-turn"))
    player.room:setPlayerMark(player, "@@lingling__xizheng_black-turn", 0)
  end,
})

xizheng:addEffect(fk.AfterCardsMove, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("@@lingling__xizheng_red-turn") > 0 then
      local ids = {}
      for _, move in ipairs(data) do
        if move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
      ids = table.filter(ids, function (id)
        return table.contains(player.room.discard_pile, id)
      end)
      ids = player.room.logic:moveCardsHoldingAreaCheck(ids)
      if #ids > 0 then
        event:setCostData(self, {cards = ids})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@lingling__xizheng_red-turn", 0)
    local cards = table.simpleClone(event:getCostData(self).cards)
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, xizheng.name, nil, true, player)
  end,
})

return xizheng
